package com.qf.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.qf.bean.Product;
import com.qf.bean.RespResult;
import com.qf.common.Const;
import com.qf.common.JSONUtils;
import com.qf.common.MybatisUtils;
import com.qf.common.StreamUtils;
import com.qf.dao.ProductDao;
//import com.qf.dao.impl.ProductDaoImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * projectName:QfeduJava2401
 * author:lcy
 * time:2024/5/9 10:05
 * description:
 */
@WebServlet("/product")
public class ProductServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setCharacterEncoding(Const.CHARACTER_ENCODING);
        response.setContentType("application/text;charset=UTF-8");
        String method = request.getParameter("method");
        List<Product> products=null;
        RespResult result = new RespResult();

        switch (method){
            case "allList":
                products=allList();
                if(products == null || products.isEmpty()){
                    result.setMsg("未获取到数据");
                    result.setCode(Const.PRODUCT_EMPTY);
                }else{
                    result.setMsg("获取列表成功");
                    result.setCode(Const.PRODUCT_SUCCESS);
                    //String s = JSONArray.toJSONString(products);
                    //result.setData(s);
                    result.setRepo(products);
                }
                break;
            //case "add":
            //    //int flag=insert(request);
            //
            //    break;
            default:
                break;
        }
        //返回前端
        PrintWriter writer=response.getWriter();
        writer.write(JSON.toJSONString(result));
        writer.flush();
        writer.close();
    }

    //public int insert(HttpServletRequest req){
        //从resopnse获取数据
        //String pno = req.getParameter("pno");
        //String pname = req.getParameter("pname");
        //String paddr = req.getParameter("paddr");
        //String pdate = req.getParameter("pdate");
        //String penterdate = req.getParameter("enterdate");
        //int pnumber = Integer.parseInt(req.getParameter("pnumber"));
        //Product product=new Product(-1,pno,pname,paddr,pdate,penterdate,pnumber);
        //ProductDao productDao=new ProductDaoImpl();
        //return productDao.insert(product);
    //}

    public List<Product> allList(){
        //ProductDao productDao=new ProductDaoImpl();
        ProductDao productDao = MybatisUtils.getMapper(ProductDao.class);
        List<Product> list= productDao.getAllList();
        MybatisUtils.commit();
        return list;
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //this.doGet(request, response);
        response.setCharacterEncoding(Const.CHARACTER_ENCODING);
        response.setContentType("application/text;charset=utf-8");



        //获取到的前端传输的数据：json格式的字符串
        String reqData = StreamUtils.readStreamData(request.getInputStream());

        //反序列化
        Product product = JSONUtils.parseObject(reqData, Product.class);

        //ProductDao productDao=new ProductDaoImpl();
        ProductDao productDao = MybatisUtils.getMapper(ProductDao.class);
        int flag = productDao.insert(product);
        MybatisUtils.commit();
        //String jsonStr;
        int code = flag==1?Const.PRODUCT_ENTER_SUCCESS:Const.PRODUCT_ENTER_FAILED;
        String msg = flag==1?"新增入库成功":"新增入库失败";
        //jsonStr =JSONUtils.toJSONString(code,msg);

        //返回前端
        PrintWriter writer=response.getWriter();
        writer.write(JSONUtils.toJSONString(code,msg));
        writer.flush();
        writer.close();
    }
}


